package xyz.chaobei.thread.simple;

/**
 * @copyright (C), 2015-2020
 * @fileName: LockAddThread
 * @author: MRC
 * @date: 2021/1/26 20:59
 * @description:
 */
public class LockAddThread {

    public static void main(String[] args) {
        ThreadC c = new ThreadC();
        ThreadD d = new ThreadD();

        c.start();
        d.start();
    }
}

class ConstantsB {
    public static int count = 0;
    public static final Object lock = new Object();
    public static final Object lockB = new Object();
}

class ThreadC extends Thread {
    @Override
    public void run() {
        for (int i = 0; i <= 1000; i++) {
            // 关键位置加一个锁
            synchronized (ConstantsB.lock) {
                ConstantsB.count++;
            }
        }
        System.out.println("ThreadA is end val=" + ConstantsB.count);
    }
}

class ThreadD extends Thread {
    @Override
    public void run() {
        for (int i = 0; i <= 1000; i++) {
            // 关键位置加一个锁
            synchronized (ConstantsB.lock) {
                ConstantsB.count--;
            }
        }
        System.out.println("ThreadB is end val=" + ConstantsB.count);
    }
}